package com.shop.util;

import org.apache.log4j.Logger;

import com.g3net.tool.StringUtils;

/**
 * vps 转换
 * @author yuxiaowei
 *
 */
public class VPSTransfer {
	private static Logger log = Logger.getLogger(VPSTransfer.class);
	private String oldvps=null;
	private String[] cols=null;
	/**
	 * 新版vps格式：0协议号#1大平台#2子平台#3机型#4IMEI#5打包id#6分辨率#7版本号#8手机号#9平台（麦贝或者欢购）#10渠道id，如1#android#android1.5#GT-I5700#333344344343#1#480_800#01.00.00
	 * 返回统计使用的vps，格式为：大平台;子平台;机型;分辨率;版本号,如：android;android1.5;GT-I5700;480_800;01.00.00
	 * @param vps	 base64编码，需解码后解析
	 * @return
	 */
	public VPSTransfer(String vps) {
		// TODO Auto-generated constructor stub
		if(!StringUtils.hasText(vps))
		{
			return ;
		}
//		try {
//			vps=Base64.decodeToString(vps, "utf-8");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			log.error("vps 解码错误", e);
//			return ;
//		}
		//System.out.println(vps);
		cols=vps.split("#");
		StringBuilder sb=new StringBuilder();
		if(cols.length >= 8)//新vps
		{
			String pattern="_|-";
			sb.append(cols[1]);sb.append(";");
			sb.append(cols[2]);sb.append(";");
			sb.append(cols[3]);sb.append(";");
			sb.append(cols[6]);sb.append(";");
			sb.append(cols[7]);
			oldvps=sb.toString();
		}else{
			oldvps=vps;
		}
		
	}
	
	public  String transfer(){
		return oldvps;
	}
	public String getIMEI()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[4];
		}
		return null;
	}
	public String getProId()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[5];
		}
		return null;
	}
	public String getResolution()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[6];
		}
		return null;
	}
	public String getResolution_Width()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[6].split("_")[0];
		}
		return null;
	}
	public String getResolution_Height()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[6].split("_")[1];
		}
		return null;
	}
	public String getVersion()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return cols[7];
		}
		return null;
	}
	public String getChannalId()
	{
		if(cols!=null&&cols.length >= 10)
		{
			return cols[10];
		}
		return null;
	}
	public boolean isNewVps()
	{
		if(cols!=null&&cols.length >= 8)
		{
			return true;
		}
		return false;
	}
	public static void main(String[] args) {
		String s="1#android#android1.5#GT-I5700#333344344343#1#480_800#01.00.008#手机号#9平台（麦贝或者欢购）#10渠道id";
		try {
//			s=Base64.encodeString(s, "utf-8");
//			System.out.println(s);
//			s=EscapeUtil.unescapeUrl("MSNhbmRyb2lkI2FuZHJvaWQyLjEjbnVsbCNya2Ryb2lkX2RldmljZSMzIzgwMF80ODAjMDEuMDAu%0AMDA%3D","utf-8");
//			System.out.println(Base64.decodeToString(s, "utf-8"));
			VPSTransfer v=new VPSTransfer(s);
			System.out.println(v.transfer());
			System.out.println(v.getResolution_Width());
			System.out.println(v.getVersion());
			System.out.println(v.getIMEI());
			System.out.println(v.getProId());
			System.out.println(v.getResolution());
			System.out.println(v.getChannalId());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//s="1#MIDP2.0# #JS11279878938456190114656077#111#240_309#05.00.00";
//		s="SYN_NOKIAN78-03.00??21";
		
	}
}
